package com.citcc.web.controller.business;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.citcc.business.bean.CdzStation;
import com.citcc.business.bean.UserOrderEntity;
import com.citcc.business.service.CdzStationService;
import com.citcc.common.utils.DateUtils;
import com.citcc.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.citcc.business.bean.CdzIndent;
import com.citcc.business.service.CdzIndentService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;

@Controller
@RequestMapping("/cdzIndent")
public class CdzIndentController  extends BaseController {

    private String prefix = "business/cdzIndent";

    @Autowired
    private CdzIndentService cdzIndentService;

    @Autowired
    private CdzStationService cdzStationService;
    @RequiresPermissions("business:cdzIndent:view")
    @GetMapping("/main")
    public String vehicle(ModelMap map){
        CdzStation station = new CdzStation();
        station.setOrgId(getSysUser().getDeptId());
        map.put("stations",cdzStationService.selectCdzStationAll(station));
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzIndent:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserOrderEntity userOrderEntity){
        startPage();
        if (userOrderEntity.getOrgId()==null||"".equals(userOrderEntity.getOrgId())) {
            userOrderEntity.setOrgId(getSysUser().getDeptId());
        }
        List<UserOrderEntity> list=cdzIndentService.selectAllList(userOrderEntity);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap map)
    {
        return prefix + "/add";
    }


    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("indent",cdzIndentService.getCdzIndent(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzIndent:edit")
    @Log(title = "交易订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzIndent indent)
    {
        indent.setUpdateBy(ShiroUtils.getLoginName());
        indent.setUpdateTime(new Date());
        return toAjax(cdzIndentService.update(indent));
    }

    @RequiresPermissions("business:cdzIndent:remove")
    @Log(title = "交易订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            Integer e =cdzIndentService.deleteOrder(id);
            return toAjax(e);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    @RequiresPermissions("business:cdzIndent:export")
    @Log(title = "订单导出", businessType = BusinessType.UPDATE)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserOrderEntity userOrderEntity)
    {
        if (userOrderEntity.getOrgId()==null||"".equals(userOrderEntity.getOrgId())) {
            userOrderEntity.setOrgId(getSysUser().getDeptId());
        }
        List<UserOrderEntity> list=cdzIndentService.selectAllList(userOrderEntity);
        ExcelUtil<UserOrderEntity> util = new ExcelUtil<UserOrderEntity>(UserOrderEntity.class);
        return util.exportExcel(list, DateUtils.dateTimeNow()+"充电订单");
    }

}
